from sqlalchemy.dialects.mysql import MEDIUMTEXT

from app.models import BaseModel
from tools import db


class LogModel(db.Model, BaseModel):
    __tablename__ = "api_log"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    ip = db.Column(db.BIGINT, index=True, nullable=True, comment="请求来源IP")
    url_path = db.Column(db.String(255), nullable=True, index=True, comment="请求路径")
    method = db.Column(db.String(20), nullable=True, comment="请求方法")

    status_code = db.Column(db.Integer, nullable=True, comment="状态码")
    pid = db.Column(db.Integer, index=True, nullable=False)
    thread_id = db.Column(db.String(64), index=True, nullable=False)
    level = db.Column(
        db.Enum("ERROR", "INFO", "WARNING", "DEBUG"),
        default="INFO",
        nullable=False,
        comment="日志级别",
    )
    operator_name = db.Column(
        db.String(120), nullable=True, index=True, comment="操作用户名称"
    )
    func_name = db.Column(db.String(120), index=True, nullable=True, comment="方法名")
    file_lineno = db.Column(db.Integer, index=True, nullable=True, comment="文件行号")


class LogDetailModel(db.Model, BaseModel):
    __tablename__ = "api_detail_log"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    log_id = db.Column(db.Integer, unique=True)

    headers = db.Column(db.JSON, nullable=True, comment="请求头")
    message = db.Column(MEDIUMTEXT, comment="日志信息")
    traceback = db.Column(db.Text, comment="异常堆栈")
